<?php

/**
 * 获取环境变量值
 * @param string $name
 * @param mixed $default
 * @return array|bool|mixed|string|null
 */
if (!function_exists('env')) {
    function env(string $name, $default = null)
    {
        $value = getenv($name);
        if ($value === 'true') {
            return true;
        } elseif ($value === 'false') {
            return false;
        }

        return $value ?: $default;
    }
}

/**
 * 数组查找
 * @param mixed $search
 * @param array $array
 * @param bool $returnKey
 * @return array|mixed|null
 */
function array_find($search, array $array, bool $returnKey = false)
{
    $search = is_callable($search) ? $search : function ($value, $key) use ($search) {
        return strpos($value, $search) !== false;
    };

    foreach ($array as $key => $value) {
        if ($search($value, $key)) {
            return $returnKey ? [$value, $key] : $value;
        }
    }

    return null;
}


if (!function_exists('database_path')) {
    /**
     * Get the database path.
     *
     * @param string $path
     * @return string
     */
    function database_path(string $path = ''): string
    {
        return base_path('storage' . DIRECTORY_SEPARATOR . $path);
    }
}
